package cc.geguo.settree.setdemo;
import java.util.Collections;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * 作者：13687
 * 日期：2017/6/18
 * 项目：JavaSetTree
 * 描述：二叉树排序
 *
 */
public class TreeSetTest {
	public static void main(String[] args) {
//		strTree();
		personTree();
	}

	private static void personTree() {
		TreeSet<Person2> treeSet = new TreeSet<>();
		treeSet.add(new Person2("geguo",17));
		treeSet.add(new Person2("geguo",13));
		treeSet.add(new Person2("geguo",19));
		treeSet.add(new Person2("geguo",18));
		treeSet.add(new Person2("geguo",16));

		Iterator it = treeSet.iterator();
		while (it.hasNext()){
			System.out.println(it.next());
		}
	}

	/**
	 * 字符串进行排序
	 */
	private static void strTree() {
		TreeSet treeSet = new TreeSet();
		treeSet.add("bbb");
		treeSet.add("aba");
		treeSet.add("baa");
		treeSet.add("aaa");
		treeSet.add("abb");
		treeSet.add("aab");
		treeSet.add("bab");
		Iterator it = treeSet.iterator();
		while (it.hasNext()){
			System.out.println(it.next());
		}
//		System.out.println("tree:"+treeSet);
		//tree:[aaa, aab, aba, abb, baa, bab, bbb]

	}
}
class Person2 implements Comparable<Person2>{
	public String name;
	public int age;
	public Person2(String name, int age) {
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person2{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}

	@Override
	public int compareTo(Person2 o) {
		if (this.age>o.age)
			return 1000;
		else if (this.age<o.age)
			return -1000;
		else if (this.age == o.age)
			return this.name.compareTo(o.name);
		return -1;
	}
	@Override
	public int hashCode() {
		return name.hashCode()+age*12;
	}



	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Person)){
			return false;
		}

		if (this.name.equals(((Person) obj).name)&&this.age == ((Person) obj).age)
			return true;
		else return false;
	}
}